<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
include_once("../libs/setup.php");
include_once("../libs/helper.php");

session_start();

if (!check_admin_session()) {
    header("Location: signin.php");
    return;
}
$smarty->assign('wwwUrl', '../');
assign_home_header($smarty);

function is_valid_candidature_type($typeID, mysqli &$mysqli, Smarty &$smarty) {
    $query = "select * from noi_dung_ung_tuyen where `Ma`={$typeID}";
    $result = $mysqli->query($query);
    if ($result) {
        if ($result->num_rows > 0) {
            return true;
        }
        return false;
    }
    return false;
}

function load_candidature($typeID, mysqli &$mysqli, Smarty &$smarty) {

    $query1 = "select count(*) from ung_tuyen ";
    if (isset($typeID))
        $query1.=" where `NoiDungUngTuyen`={$typeID}";


    $result1 = $mysqli->query($query1);

    if ($result1) {
        $row = $result1->fetch_array();
        $row_count = $row[0];

        if (isset($_REQUEST['page']))
            $page_number = preg_replace('#[^0-9]#i', '', $_REQUEST['page']);
        else
            $page_number = 1;
        $items_per_page = 20;
        if ($row_count == 0) {
            $smarty->assign('is_display_error', 'block');
            $smarty->assign('message', 'Hiện chưa có ứng tuyển nào cả.');
            $last_page = 1;
        } else {

            $last_page = ceil($row_count / $items_per_page);

            if ($page_number < 1) {
                $page_number = 1;
            } else if ($page_number > $last_page) {
                $page_number = $last_page;
            }
        }


        mysqli_free_result($result1);
        $limit = 'LIMIT ' . ($page_number - 1) * $items_per_page . ',' . $items_per_page;
        $query2 = "select * from `ung_tuyen` inner join `user` on ung_tuyen.`MaUser`=`user`.`Ma` inner join noi_dung_ung_tuyen on noi_dung_ung_tuyen.`Ma`=ung_tuyen.`NoiDungUngTuyen` ";
        if (isset($typeID))
            $query2.="where ung_tuyen.`NoiDungUngTuyen`={$typeID} ";
        $query2.= "ORDER BY ung_tuyen.`ThoiDiemUngTuyen` ASC " . $limit;
        $result2 = $mysqli->query($query2);
        if ($result2) {
            $accounts = array();
            while ($row = $result2->fetch_array()) {
                $accounts[] = array('Ma' => $row[0],
                    'MaUser' => $row['MaUser'],
                    'Email' => $row['Email'],
                    'HoTen' => $row['HoTen'],
                    'NoiDungUngTuyen' => $row['NoiDung'],
                    'ThoiDiemUngTuyen'=>$row['ThoiDiemUngTuyen'],
                    'TinhTrang' => $row['TinhTrang']
                );
            }
            $smarty->assign('candidates', $accounts);
            $smarty->assign('page_count', $last_page);
            $smarty->assign('page_current', $page_number);
        }
        if ($mysqli->error) {
            system_failed($mysqli->error . " sql: {$query2}", $smarty);
            return false;
        }
    }
    return true;
}

function load_candidature_type(mysqli &$mysqli, Smarty &$smarty) {
    $query = "select * from noi_dung_ung_tuyen";
    $result = $mysqli->query($query);
    if ($result) {
        $candidature_types = array();
        while ($row = $result->fetch_assoc()) {
            $candidature_types[] = array('Ma' => $row['Ma'],
                'NoiDung' => $row['NoiDung']);
        }
        $smarty->assign('candidature_types', $candidature_types);
    }
    if ($mysqli->error) {
        system_failed(NULL, $smarty);
        return false;
    }
    return true;
}

function accept_candidature($id, mysqli &$mysqli, Smarty &$smarty) {
    $sql = "update ung_tuyen set `TinhTrang`=1 where `Ma`={$id}";
    $result = $mysqli->query($sql);
    if ($result) {
        $smarty->assign('is_display_error', 'block');
        $smarty->assign('type_message', 'success');
        $smarty->assign('message', 'Duyệt thành công hồ sơ.');
        $query = "select * from ung_tuyen left join noi_dung_ung_tuyen on ung_tuyen.`NoiDungUngTuyen` = noi_dung_ung_tuyen.`Ma` left join `user` on ung_tuyen.`MaUser`=`user`.`Ma` where ung_tuyen.`Ma`={$id}";
        $result2 = $mysqli->query($query);
        if ($result2) {
            while ($row = $result2->fetch_assoc()) {
                $name = $row['HoTen'];
                $email = $row['Email'];
                $applyContent = $row['NoiDung'];
                $arr_email = array(array($email, $name));
                $subject = "Vinastemcelllab - Thong bao ho so ung tuyen";
                $message = 'Xin chào ' . $name . ', hồ sơ ứng tuyển với nội dung \"' . $applyContent . '\" của bạn đã được chấp nhận. Chúng tôi sẽ liên hệ với bạn qua điện thoại trong thời gian sớm nhất. \r\nPTN Nghiên cứu và ứng dụng Tế bào gốc';
                send_mail($subject, $arr_email, $message);
            }
        }
    }
    if ($mysqli->error) {
        system_failed(NULL, $smarty);
        return false;
    }
    return true;
}

function refuse_candidature($id, mysqli &$mysqli, Smarty &$smarty) {
    $sql = "update ung_tuyen set `TinhTrang`=-1 where `Ma`={$id}";
    $result = $mysqli->query($sql);
    if ($result) {
        $smarty->assign('is_display_error', 'block');
        $smarty->assign('type_message', 'success');
        $smarty->assign('message', 'Từ chối thành công hồ sơ.');
    }
    if ($mysqli->error) {
        system_failed(NULL, $smarty);
        return false;
    }
    return true;
}

$mysqli = connect_database($smarty);
$typeID = NULL;
if (isset($_REQUEST['id']) && isset($_REQUEST['action'])) {
    if ($_REQUEST['action'] == 'accept') {

        if (!accept_candidature($_REQUEST['id'], $mysqli, $smarty))
            return;
    }
    if ($_REQUEST['action'] == 'refuse') {
        if (!refuse_candidature($_REQUEST['id'], $mysqli, $smarty))
            return;
    }
}
if (isset($_REQUEST['type'])) {
    $typeID = $_REQUEST['type'];
    if (!is_valid_candidature_type($typeID, $mysqli, $smarty)) {
        $smarty->assign('is_display_error', 'block');
        $smarty->assign('message', 'Loại ứng tuyển không tồn tại.');
        $typeID = NULL;
    }
}

if (!load_candidature_type($mysqli, $smarty))
    return;

if (!load_candidature($typeID, $mysqli, $smarty))
    return;
$smarty->assign('candidature_type', $typeID);
$smarty->display('admin/candidature.tpl');

